<ui:composition template="../layout/layout.xhtml"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:p="http://primefaces.org/ui"
	xmlns:rich="http://richfaces.org/rich"
	xmlns:a4j="http://richfaces.org/a4j"
	>

	<ui:define name="title">
		<h:outputText value="Documento" />
	</ui:define>

	<ui:define name="content">
		<div id="path">
			<h1>Documento</h1>
			<hr />
			<p id="crumb">Administração / Documento</p>
		</div>
		<h2>Cadastro de Documento</h2>
		<h:form name="cadastro" prependId="false" onsubmit="return validaForm()"  rendered="#{sessionScope.usuario.admin}">

			<h:outputLabel value="Classe:" for="classe" />
			<h:selectOneMenu id="classe"
				value="#{documentoMBean.documento.classe}" required="true"
				requiredMessage="* O campo [Classe de documento] é obrigatório!">
				<f:selectItems value="#{classeDocumentoMBean.listaClasseDocumento}"
					var="#{classe}" itemValue="#{classe}" />
				<f:converter converterId="simpleEntityConverter" />
			</h:selectOneMenu>

			<h:message for="classe" showSummary="true" showDetail="false"
				class="error" />
			<br />


			<h:outputLabel value="Tipo:" for="tipo" />
			<h:selectOneMenu id="tipo" value="#{documentoMBean.documento.tipo}"
				required="true"
				requiredMessage="* O campo [Tipo de documento] é obrigatório!">
				<f:selectItem value="#{tipoDocumentoMBean.valorPadrao}" noSelectionOption="true"  />
				<f:selectItems value="#{tipoDocumentoMBean.listaTipoDocumento}"
					var="#{tipo}" itemValue="#{tipo}"  />
				<f:converter converterId="simpleEntityConverter" />
				<p:ajax event="change" update="comp"
					listener="#{documentoMBean.actionAtualizaTipo}" />
			</h:selectOneMenu>

			<h:message for="classe" showSummary="true" showDetail="false"
				class="error" />
			<br />
				<h:panelGroup id="comp">
				<ui:repeat var="m" value="#{documentoMBean.metavalores}">

					<h:outputLabel value="#{m.metadado.descricao}: " />
					<rich:calendar rendered="#{m.metadado.tipo=='Data'}" value="#{m.valor2}" 
						datePattern="dd/MM/yyyy" enableManualInput="false" buttonDisabledIcon="true" >
					</rich:calendar>
					
					<h:inputText
						rendered="#{m.metadado.tipo=='Numerico'}"
						id="metadadon" value="#{m.valor}"
						required="true"
						requiredMessage="* O campo [Descrição] é obrigatório!"
						converterMessage="Digite apenas números neste campo">
						<f:validator validatorId="br.edu.ifrn.controller.NumberValidator"/> 
					</h:inputText>
					<h:inputText
						rendered="#{m.metadado.tipo=='Texto'}"
						id="metadadot" value="#{m.valor}"
						required="true"
						requiredMessage="* O campo [Descrição] é obrigatório!" />
					<br />
				</ui:repeat>
			</h:panelGroup>
			
			<h:outputLabel value="Arquivo Digital:"  />
			<h:panelGroup id="up">
			<h:outputLabel value="#{documentoMBean.fileUpload.name}" rendered="#{documentoMBean.arquivoSelecionado}" />
			<a4j:commandButton id="selectFile" onclick="#{rich:component('uploadPanel')}.show()"
				value="Selecionar" rendered="#{!documentoMBean.arquivoSelecionado}" />

			<a4j:commandButton id="altFile" onclick="#{rich:component('uploadPanel')}.show()"
				value="Alterar" rendered="#{documentoMBean.arquivoSelecionado}" />
			</h:panelGroup>


			<br />
			<h:outputLabel value="Data de Entrada:" for="dataEntrada" />
			<rich:calendar id="dataEntrada"
				value="#{documentoMBean.documento.dataEntrada}"
				datePattern="dd/MM/yyyy">
				<h:message for="dataEntrada" showSummary="true" showDetail="false"
					class="error" />
			</rich:calendar>
			<br />
			<br />
			
			<h:commandButton class="button check" value="Salvar"
				action="#{documentoMBean.actionSalvar}" />
			<h:commandButton class="button clear" value="Limpar"
				action="#{documentoMBean.actionLimpar}" immediate="true">
				<f:ajax render="@form"></f:ajax>
			</h:commandButton>
		</h:form>
			  <rich:popupPanel id="uploadPanel" autosized="true">
				<f:facet name="header">
					<h:outputText value="Upload de Arquivo digital" style="float:left;" />
					<h:graphicImage value="/resources/images/modalclose.png"
						style="cursor:pointer; float:right;"
						onclick="#{rich:component('uploadPanel')}.hide()" />
				</f:facet>
				<h:form>
        <h:panelGrid columns="2" columnClasses="top,top">
            <rich:fileUpload fileUploadListener="#{documentoMBean.listener}" 
                id="upload" maxFilesQuantity="1" listHeight="80px" style="width:280px;"
                deleteLabel="Excluir" clearAllLabel="Limpar tudo" addLabel="Selecionar" clearLabel="Limpar"
                doneLabel="Upload Concluído"  >
                <a4j:ajax event="uploadcomplete" execute="@none" render="info, up" />
            </rich:fileUpload><br/>
            <h:panelGroup id="info" layout="block" style="width:280px;" >
                <rich:panel bodyClass="info">
                    <f:facet name="header">
                        <h:outputText value="Informações do arquivo" />
                    </f:facet>
                    <h:outputText value="Arquivo ainda não carregado"
                        rendered="#{documentoMBean.fileUpload == null}" />
                        <rich:panel bodyClass="rich-laguna-panel-no-header" rendered="#{documentoMBean.fileUpload != null}" >
                            <h:panelGrid columns="2">
                                <h:panelGrid columns="2">
                                    <h:outputText value="Nome:" />
                                    <h:outputText value="#{documentoMBean.fileUpload.name}" />
                                    <h:outputText value="Tamanho (bytes):" />
                                    <h:outputText value="#{documentoMBean.fileUpload.size}" />
                                </h:panelGrid>
                            </h:panelGrid>
                            <a4j:commandButton action="#{documentoMBean.clearUploadData}"
                    render="info, upload, up" value="Remover"
                    rendered="#{documentoMBean.fileUpload != null}" />
                    <br />
                        </rich:panel>
                </rich:panel>
                <br />
                <a4j:commandButton onclick="#{rich:component('uploadPanel')}.hide()"
                    render="info, upload" value="Fechar"
                    />
            </h:panelGroup>
        </h:panelGrid>
    </h:form>
			</rich:popupPanel>
				
			

		<div class="spacer"></div>

		<h:form  rendered="#{sessionScope.usuario.admin}">
			<h:dataTable styleClass="tabdefault"
				value="#{documentoMBean.listaDocumento}" var="d" >
				
				
				<h:column>
					<f:facet name="header">
						<h:outputText value="Id"  />
					</f:facet>
					<h:outputText value="#{d.id}"  />
				</h:column>
				<h:column sortBy="#{d.classe}">
					<f:facet name="header">
						<h:outputText value="Classe" />
					</f:facet>
					<h:outputText value="#{d.classe}" />
				</h:column>
				<h:column sortBy="#{d.tipo}">
					<f:facet name="header">
						<h:outputText value="Tipo" />
					</f:facet>
					<h:outputText value="#{d.tipo}" />
				</h:column>
				<h:column sortBy="#{d.arquivo.nome}">
					<f:facet name="header">
						<h:outputText value="Arquivo Digital" />
					</f:facet>
					<h:outputText value="#{d.arquivo.nome} " />
					
				</h:column>
				<h:column sortBy="#{d.dataCadastro}">
					<f:facet name="header">
						<h:outputText value="Data de Cadastro" />
					</f:facet>
					<h:outputText value="#{d.dataCadastro}" >
						<f:convertDateTime for="dataCadastro" pattern="dd/MM/yyyy" />
					</h:outputText>
				</h:column>
				<h:column>
					<f:facet name="header">
						<h:outputText value="Data de Entrada" />
					</f:facet>
					<h:outputText id="dataEntrada" value="#{d.dataEntrada}"  >
						<f:convertDateTime for="dataEntrada" pattern="dd/MM/yyyy" />
					</h:outputText>
				</h:column>
				<h:column>
					<f:facet name="header">
						<h:outputText value="Status" />
					</f:facet>
					<h:outputText value="Descartado" rendered="#{d.descartado}" />
					<h:outputText value="Disponível" rendered="#{!d.descartado}" />
					<h:outputText value=" - Passível de Descarte" rendered="#{!d.descartado and d.passivelDescarte}" />
				</h:column>
				<h:column>
					<f:facet name="header">
						<h:outputText value="Ação" />
					</f:facet>
					<h:commandLink value="" title="Editar">
						<f:setPropertyActionListener target="#{documentoMBean.documento}"
							value="#{d}" />
						<h:graphicImage library="images" name="ico_edit16x16.png" />
					</h:commandLink>

					<h:commandLink value="" action="#{documentoMBean.actionRemover}"
						title="Remover">
						<f:setPropertyActionListener target="#{documentoMBean.documento}"
							value="#{d}" />
						<h:graphicImage library="images" name="ico_delete16x16.png" />
					</h:commandLink>
					<a4j:commandLink value="" title="Download" action="#{documentoMBean.actionDownload}" onclick="#{rich:component('downloadPanel')}.show()"
						render="fileName">
						<h:graphicImage library="images" name="download16x16.png" />
						<f:setPropertyActionListener target="#{documentoMBean.documento}"
							value="#{d}" />
					</a4j:commandLink>
					<h:commandLink value="" action="#{documentoMBean.descartarDocumento}"
						title="Descartar" rendered="#{d.passivelDescarte}" >
						<f:setPropertyActionListener target="#{documentoMBean.documento}"
							value="#{d}" />
						<h:graphicImage library="images" name="discard16x16.png" />
					</h:commandLink>
				</h:column>
			</h:dataTable>
		</h:form>
		<rich:popupPanel id="downloadPanel" autosized="true" >
			<f:facet name="header">
				<h:outputText value="Download de Arquivo digital" style="float:left;" />
				<h:graphicImage value="/resources/images/modalclose.png"
					style="cursor:pointer; float:right;"
					onclick="#{rich:component('downloadPanel')}.hide()" />
			</f:facet>
			<h:form>
			<h:outputLabel value="Arquivo: " />
			<h:outputLabel id="fileName" value="#{documentoMBean.fileDownload.name}" />
			<br />
			<h:commandButton value="Download" onclick="#{rich:component('downloadPanel')}.hide()" ajax="false">
				<p:fileDownload value="#{documentoMBean.fileDownload}" />
			</h:commandButton>
			<h:commandButton value="Cancelar" onclick="#{rich:component('downloadPanel')}.hide()" ajax="false" />
			</h:form>
			
		</rich:popupPanel>
	</ui:define>
</ui:composition>